import Tween from "../anim/Tween";
import { emptyStr } from "../common/GameCost";
import Pool from "../pool/Pool";

export default class BadgeItem extends Laya.Sprite {

    protected _skin: string;
    protected _state: boolean;

    static create() {
        let item = Pool.get(BadgeItem);
        return item;
    }

    setSkin(skin: string) {
        if (this._skin === skin) {
            return;
        }
        this.loadImage(skin);

        return this;
    }

    setAnchor(x: number, y: number) {
        this.pivot(x, y);
        return this;
    }

    setActive(b: boolean, recover: boolean = false) {
        if (this._state === b) {
            return;
        }

        this.visible = b = this._state;

        if (b) {
            Tween.get<Laya.Sprite>(this, { loop: true })
                .to({ scaleX: 1.3, scaleY: 1.3 }, 200)
                .to({ scaleX: 0.8, scaleY: 0.8 }, 300)
                .to({ scaleX: 1, scaleY: 1 }, 200)
                .wait(200);
        } else {
            Tween.clearAll(this);
            recover && this.recover();
        }

        return this;
    }

    onDestroy(): void {
        Tween.clearAll(this);
    }

    recover() {
        this._skin = emptyStr;
        Pool.put(this);
    }
}